﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using NUnit.Framework;
using Smith.BuildExtensions.ConfigurationTransformation;
using Assert = NUnit.Framework.Assert;

namespace BuildExtensionsTests
{
    [TestClass]
    public class ConfigReaderTest
    {

        [TestMethod]
        public void GetBaseConfigShouldThrowInvalidOperationExceptionIfNoConfigurationExist()
        {
            ConfigReader reader = new ConfigReader("h:\\temp", "App.config.template.config", "Debug2","BuildExtensionsTests");

            Assert.Throws<InvalidOperationException>(() => reader.GetBaseConfig());
        }

        [TestMethod]
        public void GetBaseConfigShouldReturnConfiguration()
        {
            ConfigReader reader = new ConfigReader(Environment.CurrentDirectory, "App.config.template.config", "Debug", "BuildExtensionsTests");

            ConfigSettings configSettings = reader.GetBaseConfig();

            Assert.That(configSettings, Is.Not.Null);
        }

        [TestMethod]
        public void GetConfigurationTemplateShouldThrowInvalidOperationExceptionIfTemplateDoesNotExist()
        {
            ConfigReader reader = new ConfigReader("h:\\temp", "App.config.template99.config", "Debug", "BuildExtensionsTests");

            Assert.Throws<InvalidOperationException>(() => reader.GetConfigurationTemplate());
        }

        [TestMethod]
        public void GetConfigurationTemplateShouldReturnContentsOffileIfItExist()
        {
            const string Contents = @"this is the file contents";
            string templateFileName = Path.Combine(Environment.CurrentDirectory, "test.template.config");
            try
            {
                
                File.WriteAllText(templateFileName, Contents);
                ConfigReader reader = new ConfigReader("h:\\temp", templateFileName, "Debug", "BuildExtensionsTests");

                string contentsOfConfig = reader.GetConfigurationTemplate();

                Assert.That(contentsOfConfig, Is.EqualTo(Contents));
            }
            finally
            {
                try
                {
                    File.Delete(templateFileName);
                }
                catch
                {
                }
            }
        }

        [TestMethod]
        public void TryGetBuildConfigShouldReturnFalseIfConfigDoesNotExist()
        {
            ConfigReader reader = new ConfigReader(Environment.CurrentDirectory, "App.config.template.config", "Debug2", "BuildExtensionsTests");

            ConfigSettings configSettings;
            Assert.That(reader.TryGetBuildConfigurationConfig(out configSettings), Is.False);
        }

        [TestMethod]
        public void TryGetBuildConfigShouldReturnTrueIfConfigDoesExist()
        {
            ConfigReader reader = new ConfigReader(Environment.CurrentDirectory, "App.config.template.config", "Debug", "BuildExtensionsTests");

            ConfigSettings configSettings;
            Assert.That(reader.TryGetBuildConfigurationConfig(out configSettings), Is.True);
        }

        [TestMethod]
        public void TryGetProjectConfigShouldReturnTrueIfConfigDoesExist()
        {
            ConfigReader reader = new ConfigReader(Environment.CurrentDirectory, "App.config.template.config", "Debug", "BuildExtensionsTests");

            ConfigSettings configSettings;
            Assert.That(reader.TryGetProjectConfigurationConfig(out configSettings), Is.True);
        }

        [TestMethod]
        public void TryGetProjectConfigShouldReturnFalseIfConfigDoesExist()
        {
            ConfigReader reader = new ConfigReader(Environment.CurrentDirectory, "App.config.template.config", "Debug", "BuildExtensionsTests009");

            ConfigSettings configSettings;
            Assert.That(reader.TryGetProjectConfigurationConfig(out configSettings), Is.False);
        }
    }
}
